import { fileURLToPath, URL } from 'node:url'
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
import path from 'path'
import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import moment from 'moment'

const buildTime = moment().format('YYMMDD-HHmm')
// https://vitejs.dev/config/
export default defineConfig(({ mode }) => {
  const env = loadEnv(mode, process.cwd(), '')
  const { VITE_DOMAIN } = env
  return {
    base: './',
    plugins: [
      vue(),
      createSvgIconsPlugin({
        // 指定需要缓存的图标文件夹
        iconDirs: [path.resolve(process.cwd(), 'src/icons/svg')],
        // 指定symbolId格式
        symbolId: 'icon-[dir]-[name]',
      }),
    ],
    resolve: {
      alias: {
        '@': fileURLToPath(new URL('./src', import.meta.url)),
      },
    },
    server: {
      //用来配置跨域
      proxy: {
        '/bimface-api': {
          target: 'https://api.bimface.com/', //目标服务器地址
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/bimface-api/, ''),
        },
        '/api': {
          target: `${VITE_DOMAIN}/vue/qygly-gateway/`, //目标服务器地址
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/api/, ''),
        },
      },
    },
    css: {
      // css预处理器
      preprocessorOptions: {
        scss: {
          // 引入 mixin.scss 这样就可以在全局中使用 mixin.scss中预定义的变量了
          // 给导入的路径最后加上 ;
          additionalData:
            '@use "@/assets/styles/element.scss" as *;@import "@/assets/styles/index.scss";',
        },
      },
    },
    build: {
      outDir: `dist/${mode}/${buildTime}`,
    },
    lintOnSave: false, //false 控制台不报错，编译通过。warning控制台提示错误，编译通过
  }
})
